from rest_framework.authentication import BaseAuthentication
from .tooljwt import ToolJWT
from human.models import UserModel
from rest_framework.response import Response
class JWTAuthentication(BaseAuthentication):
    def authenticate(self,request):
        tokendata = request.META.get('HTTP_AUTHORIZATION',None)
        if tokendata:
            token = tokendata.split(' ')[1]
            tool = ToolJWT()
            token_data = tool.check_token(token)
            print(token_data)
            userid = token_data.get('userid')
            user = UserModel.objects.filter(id=userid).first()
            return (user,None) # 返回用户对象  是一个二元组
        else:
            return None, None


    def authenticate_header(self, request):
        return 'Bearer realm="api"'
